package com.example.opengldemo.simple12_04;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.graphics.drawable.ShapeDrawable;
import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;
import com.example.opengldemo.util.ShapeFactory;


public class LoadedObjectVertexNormal
{	
    ShaderHandle shaderHandle ;
    ShaderBuffer shaderBuffer ;
    int muClipHandle;
    String mVertexShader;
    String mFragmentShader;

    
    public LoadedObjectVertexNormal(View mv, float[] vertices, float[] normals)
    {
    	initVertexData(vertices,normals);
    	intShader(mv);
    }
    

    public void initVertexData(float[] vertices,float[] normals)
    {
    	shaderBuffer = new ShaderBuffer() ;
        shaderBuffer.setVertexBuffer(vertices);
        shaderBuffer.setNormalBuffer(normals);

    }


    public void intShader(View mv)
    {

        mVertexShader= ShaderUtil.loadFromAssetsFile("simple12_04/vertex.sh", mv.getResources());

        mFragmentShader=ShaderUtil.loadFromAssetsFile("simple12_04/frag.sh", mv.getResources());

        shaderHandle = new ShaderHandle(ShaderUtil.createProgram(mVertexShader, mFragmentShader) , false) ;
        shaderHandle.build() ;
        muClipHandle=GLES30.glGetUniformLocation(shaderHandle.getProgram(), "u_clipPlane");
    }
    
    public void drawSelf(float[] clipPane)
    {


        shaderHandle.program()
                .bindMVPMaxtrixHandle()
                .bindMMaxtrixHandle()
                .bindLightHandle()
                .bindCameraHandle()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindNormalHandle(shaderBuffer.getNormalBuffer());

         GLES30.glUniform4fv(muClipHandle, 1, ShapeFactory.getFloatBuffer(clipPane));

        shaderHandle.run(GLES30.GL_TRIANGLES,  shaderBuffer.vCount);
    }
}
